<?php
// load Wordpress
require_once('../../../wp-load.php');
require_once('../../../wp-admin/includes/admin.php');
do_action('admin_init');

// load AjaxTableEditor
require_once('mate/php/lang/LangVars-en.php');
require_once('mate/php/AjaxTableEditor.php');
require_once('reg_table.php');

class RegAjax
{
	function logError($message, $file, $line)
	{
		$message = sprintf($this->langVars->errInScript,$file,$line,$message);
		var_dump($message);
		die;
	}

	function initiateEditor()
	{
		global $current_user;
		get_currentuserinfo();
		$first_name = get_usermeta($current_user->ID,'first_name');
		$last_name = get_usermeta($current_user->ID,'last_name');
		$locality = get_usermeta($current_user->ID,'locality');
		$phone = get_usermeta($current_user->ID,'phone');
		$email = $current_user->user_email;
		$tableColumns['id'] = array('display_text' => 'ID', 'perms' => 'VQSXO');
		$tableColumns['region'] = array('display_text' => 'Region', 'perms' => 'VCTXQSHO', 'req' => true);
		$tableColumns['locality'] = array('display_text' => 'Region', 'perms' => 'EVCTXQ', 'req' => true, 'join' => array('table' => 'regions', 'column' => 'id', 'display_mask' => 'regions.region', 'type' => 'left'));
		$tableName = 'regions';
		$primaryCol = 'id';
		$errorFun = array(&$this,'logError');
		$permissions = 'UEVIMQSXHO';

		$this->Editor = new RegTableEditor($tableName,$primaryCol,$errorFun,$permissions,$tableColumns);
		$this->Editor->setConfig('varPrefix','localities');
		$this->Editor->setConfig('tableTitle', 'Localities and Regions');
		$this->Editor->setConfig('tableInfo','cellpadding="1" class="mateTable"');
		$this->Editor->setConfig('orderByColumn','city');
		$this->Editor->setConfig('editRowTitle','Edit Region');
	}

	function RegAjax()
	{
		if ( ! is_user_logged_in() ) {
			$this->logError('User needs to be logged in',__FILE__, __LINE__);
		}
		if ( !current_user_can('edit_published_posts') ) {
			$this->logError('Reg error: user does not have read permissions',__FILE__, __LINE__);
		}
		if(isset($_POST['json']))
		{
			session_start();
			// Initiating lang vars here is only necessary for the logError, and mysqlConnect functions in Common.php.
			// If you are not using Common.php or you are using your own functions you can remove the following line of code.
			$this->langVars = new LangVars();

			if(ini_get('magic_quotes_gpc'))
			{
				$_POST['json'] = stripslashes($_POST['json']);
			}
			if(function_exists('json_decode'))
			{
				$data = json_decode($_POST['json']);
			}
			else
			{
				require_once('/mate/php/JSON.php');
				$js = new Services_JSON();
				$data = $js->decode($_POST['json']);
			}
			if(empty($data->info) && strlen(trim($data->info)) == 0)
			{
				$data->info = '';
			}
			$this->initiateEditor();
			$this->Editor->main($data->action,$data->info);
			if(function_exists('json_encode'))
			{
				echo json_encode($this->Editor->retArr);
			}
			else
			{
				echo $js->encode($this->Editor->retArr);
			}
		}
		else if(isset($_GET['export']))
		{
			session_start();
			ob_start();
			$this->initiateEditor();
			echo $this->Editor->exportInfo();
			header("Cache-Control: no-cache, must-revalidate");
			header("Pragma: no-cache");
			header("Content-type: application/x-msexcel");
			header('Content-Type: text/csv');
			header('Content-Disposition: attachment; filename="'.$this->Editor->tableName.'.csv"');
			exit();
		}
	}
}

$regAjax = new RegAjax();